<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="项目名称" prop="projectName">
                    <el-input class="w-[280px]" v-model="queryParams.projectName" />
                </el-form-item>
                <el-form-item label="申报条件" prop="declarationConditions">
                    <el-input class="w-[280px]" v-model="queryParams.declarationConditions" />
                </el-form-item>
                <el-form-item label="项目关键要素" prop="projectKey">
                    <el-input class="w-[280px]" v-model="queryParams.projectKey" />
                </el-form-item>
                <el-form-item label="核心要素" prop="keyElement">
                    <el-input class="w-[280px]" v-model="queryParams.keyElement" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link style="margin-right: 10px" :to="{
                    path: getRoutePath('projectBasicInformation:addProject'),
                }">
                    <el-button v-perms="['projectBasicInformation:addProject']" type="primary">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </router-link>

                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-download" />
                    </template>
                    导出
                </el-button>
                <el-button v-perms="['thread:add/edit']" type="primary">
                    <template #icon>
                        <icon name="el-icon-upload" />
                    </template>
                    导入
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <el-table-column type="selection" width="55" />
                <el-table-column label="ID" prop="id" min-width="100" sortable />
                <el-table-column label="省" prop="" min-width="50" />
                <el-table-column label="市" prop="" min-width="50" />
                <el-table-column label="区" prop="" min-width="50" />
                <el-table-column label="项目名称" prop="projectName" min-width="180" sortable show-overflow-tooltip />
                <el-table-column label="申报条件" min-width="180" sortable width="280">
                    <template v-slot="scope">
                        <!-- 显示 declarationTypeName -->
                        <span v-html="scope.row.declarationConditions.map(item => item.andsqlName).join('<br/>')"
                            class="mr-2 tag-with-ellipsis"
                            :title="scope.row.declarationConditions.map(item => item.andsqlName).join(', ')"></span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="申报条件" prop="declarationConditions" min-width="180" sortable width="500" /> -->
                <el-table-column label="文件实施时间" prop="" min-width="180" sortable />
                <el-table-column label="项目申报时间" prop="" min-width="180" sortable />
                <el-table-column label="项目关键要素" prop="projectKey" min-width="280" sortable show-overflow-tooltip />
                <el-table-column label="核心要素" prop="keyElement" min-width="280" sortable show-overflow-tooltip />
                <el-table-column label="相关公式" prop="" min-width="180" sortable />
                <el-table-column label="创建时间" prop="createTime" min-width="180" sortable />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['detail:edit']" type="primary" link @click="handleEdit(row)">
                            <router-link :to="{
                                path: getRoutePath('projectBasicInformation:matchingBy'),
                                query: {
                                    id: row.id,
                                },
                            }">
                                匹配企业
                            </router-link>
                        </el-button>
                        <el-button v-perms="['detail:edit']" type="primary" link>
                            <router-link :to="{
                                path: getRoutePath('projectBasicInformation:editProject'),
                                query: {
                                    id: row.id,
                                },
                            }">
                                编辑</router-link>
                        </el-button>
                        <el-button v-perms="['detail:del']" type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="detail">
import { detailDelete, detailLists } from "@/api/detail";
import { usePaging } from "@/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
import { getRoutePath } from "@/router";

const queryParams = reactive({
    projectName: "",
    declarationConditions: "",
    projectKey: "",
    keyElement: "",
});

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: detailLists,
    params: queryParams,
});

const handleAdd = async () => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
    showEdit.value = true;
    await nextTick();
    editRef.value?.open("edit");
    editRef.value?.getDetail(data);
};

const handleDelete = async (id: number) => {
    await feedback.confirm("确定要删除？");
    await detailDelete({ id });
    feedback.msgSuccess("删除成功");
    getLists();
};

const getDeclarationTypeNames = async (declarationConditions) => {
    debugger
    // 获取并拼接所有 declarationTypeName
    if (!declarationConditions || !Array.isArray(declarationConditions)) {
        return ''; // 防止出现数据不符合预期的情况
    }
    return declarationConditions.map(item => item.declarationTypeName).join(', ');
}

getLists();
</script>

<style scoped>
.tag-with-ellipsis {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    /* 限制显示的行数 */
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    /* 文字过长时显示省略号 */
    white-space: normal;
    /* 允许换行 */
    max-height: 5.4em;
    /* 控制两行文字的高度 */
}

.tag-with-ellipsis:hover {
    text-decoration: underline;
    /* 可选，鼠标悬停时增加下划线 */
}
</style>
